package com.zchu.labelselection;

import android.content.Context;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * author : zchu
 * date   : 2017/7/10
 * desc   :
 */

public class LabelSelectionFragment extends Fragment implements OnItemDragListener {

    private RecyclerView mRecyclerView;
    private LabelSelectionAdapter mLabelSelectionAdapter;
    private ItemTouchHelper mHelper;

    protected OnEditFinishListener mOnEditFinishListener;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mRecyclerView = new RecyclerView(inflater.getContext());
        mRecyclerView.setPadding(
                (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, getResources().getDisplayMetrics()),
                (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, getResources().getDisplayMetrics()),
                (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, getResources().getDisplayMetrics()),
                0
        );
        mRecyclerView.setClipToPadding(false);
        mRecyclerView.setClipChildren(false);
        FrameLayout frameLayout=new FrameLayout(inflater.getContext());
        frameLayout.addView(mRecyclerView);
        return frameLayout;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnEditFinishListener) {
            mOnEditFinishListener = (OnEditFinishListener) context;
        }
    }

    protected void initData(List<Label> selectedLabels, List<Label> unselectedLabels, List<Label> alwaySelectedLabels){
        final ArrayList<LabelSelectionItem> labelSelectionItems = new ArrayList<>();
        labelSelectionItems.add(new LabelSelectionItem(LabelSelectionItem.TYPE_LABEL_SELECTED_TITLE, "切换栏目"));

        if (alwaySelectedLabels != null && alwaySelectedLabels.size() > 0) {
            for (Label alwaySelectedLabel : alwaySelectedLabels) {
                labelSelectionItems.add(new LabelSelectionItem(LabelSelectionItem.TYPE_LABEL_ALWAY_SELECTED, alwaySelectedLabel));
            }
        }

        if (selectedLabels != null && selectedLabels.size() > 0) {
            for (Label selectedLabel : selectedLabels) {
                labelSelectionItems.add(new LabelSelectionItem(LabelSelectionItem.TYPE_LABEL_SELECTED, selectedLabel));
            }
        }
        labelSelectionItems.add(new LabelSelectionItem(LabelSelectionItem.TYPE_LABEL_UNSELECTED_TITLE, "点击添加更多标签"));

        if (unselectedLabels != null && unselectedLabels.size() > 0) {
            for (Label unselectedLabel : unselectedLabels) {
                labelSelectionItems.add(new LabelSelectionItem(LabelSelectionItem.TYPE_LABEL_UNSELECTED, unselectedLabel));
            }
        }

        mLabelSelectionAdapter = new LabelSelectionAdapter(labelSelectionItems);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 4);
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                int itemViewType = mLabelSelectionAdapter.getItemViewType(position);
                return itemViewType == LabelSelectionItem.TYPE_LABEL_SELECTED || itemViewType == LabelSelectionItem.TYPE_LABEL_UNSELECTED || itemViewType == LabelSelectionItem.TYPE_LABEL_ALWAY_SELECTED ? 1 : 4;
            }
        });

        mRecyclerView.setLayoutManager(gridLayoutManager);
        mRecyclerView.setAdapter(mLabelSelectionAdapter);

        ItemDragHelperCallBack callBack = new ItemDragHelperCallBack(this);
        mLabelSelectionAdapter.setOnChannelDragListener(this);
        mLabelSelectionAdapter.setOnEditFinishListener(mOnEditFinishListener);
        mHelper = new ItemTouchHelper(callBack);
        mHelper.attachToRecyclerView(mRecyclerView);
    }

    @Override
    public void onItemMove(int starPos, int endPos) {
        List<LabelSelectionItem> data = mLabelSelectionAdapter.getData();
        LabelSelectionItem labelSelectionItem = data.get(starPos);
        //先删除之前的位置
        data.remove(starPos);
        //添加到现在的位置
        data.add(endPos, labelSelectionItem);

        mLabelSelectionAdapter.notifyItemMoved(starPos, endPos);
    }

    @Override
    public void onStarDrag(RecyclerView.ViewHolder viewHolder) {
        mHelper.startDrag(viewHolder);
    }

    public boolean cancelEdit() {
        return mLabelSelectionAdapter.cancelEdit();
    }
}
